<?php

require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/tdao/Class_T_MON_CELL.php' );

Class_PWD::valid_auth(AOKI_AUTH_MON_CELL,$login['staff_auth']);
$dao = new Class_T_MON_CELL();
$isG7  = FALSE;
$isG23  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}
$smarty->assign('isG7', $isG7 );
$authtg2  =  "none" ;
$authtg3  =  "none" ;
$authtg4  =  "none" ;
$cellsort = $aoki_cell_shop_sort;

//$zoneuser = array();
//$zoneuser = Class_M_OTHER::get_zone_users();

$login_auth = $login['staff_auth'];
if ($login_auth == 'G-2' ) {
	$isG23  = true;
	$aja_cd   = $login['autoid'];
	$authtg2 =  "block" ;
	$forms["sort"] = '2';
	$cellsort = array();
	$cellsort = array(2 => $aoki_cell_shop_sort[2], 4 => $aoki_cell_shop_sort[4] );
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else if ($login_auth == 'G-3' ) {
	$isG23  = true;
	$aja_cd   = $login['autoid'];
	$authtg3 =  "block" ;
	$forms["sort"] = '3';
	$cellsort = array();
	$cellsort = array(3 => $aoki_cell_shop_sort[3], 4 => $aoki_cell_shop_sort[4] );
	$zone_name = "";
	/*
	foreach ($zoneuser as $k =>$v ){
		if ( $v["userid"] == $aja_cd ) {
			$zone_name = $v["login_id"]."  ".$v["user_name"] ;
		}
		$zoneuser[$v["userid"]]  = $zone_name ;
	}
	*/
	$zoneuser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
} else {
	
	$zoneuser = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 3);
	
	$areauser = array();
	$areauser =  Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 2);
	
	/*
	$zoneuser[" "] =  PLEASE_SELECT_ARR ;
	foreach ($zoneuser as $k =>$v ){
		$zoneuser[$v["userid"]]  = $v["login_id"]."  ".$v["user_name"] ;
	}
	*/
}
$smarty->assign('isG23', $isG23 );
/*
if ($login_auth == 'G-8' ) {
	$aja_cd   = $login['autoid'];
	$authtg2 =  "none" ;
	$authtg3 =  "none" ;
}
*/
//---CSVダウンロード　処理開始
if ( isset( $_POST['csv'] ) &&  (  $_POST['csv'] == "download" ) ){
	$forms = $_POST;
	$_POST["csv"] = "";
	$err = new Class_ERROR;
	$wherearr = array();
	$formshops = NULL;
	if ($forms["sort"]) {
		if ($forms["sort"] == '2') {
			$authtg2 = "block";
			$err_mes['areauser'] = $err->check( $forms['areauser'],  array( "EXIST" ) );
			$formaja_cd = $forms['areauser'];
		}
		
		if ($forms["sort"] == '3') {
			$authtg3 = "block";
			$err_mes['zone_cd'] = $err->check( $forms['zone_cd'],  array( "EXIST" ) );
			$formaja_cd = $forms['zone_cd'];
		}
		
		if ($forms["sort"] == '4') {
			$authtg4 = "block";
			$err_mes['shop_cd'] = $err->check( $forms['shop_cd'],  array( "EXIST" ) );
			$formshops = " ('".implode("','", $forms["shop_cd"])."')";
		}
		
		if (isset($forms["shop_cd"]) && ( !empty($forms["shop_cd"]) ) ) {
			foreach ($forms["shop_cd"] as $k=>$v) {
				$forms["mutirow"] []=array("shop_cd"=> $v, "shop_name" => $forms["shop_name"][$k]);
			}
		}
	}
	
	if ( $err->clear ){
		$_POST["csv"] = "";
		$tmpfilename = "cellshop".date("Ymd_His");
		header("Content-Type: application/octet-stream"); 
		header("Content-Disposition: attachment; filename=$tmpfilename.csv");
		$matrix_cd_f =  $forms["ww1"];
		$matrix_cd_t =  $forms["ww2"];
		
		$year4 = $forms["year4"];
		$dao->setMaxMinMatrixCD($year4, $matrix_cd_f, $matrix_cd_t);
		
		$aja_cd = $aja_cd ? $aja_cd : $formaja_cd;
		
		$week = $dao->get_cell_week( $year4, NULL ,TRUE, TRUE, $matrix_cd_f, $matrix_cd_t);
		
		//$data = $dao->searchcsv( $wherearr );
		$data_base  = array();
		$indexB     = array();
		$i = 0;
		$indexB["ji"]["sale"] = $i;
		$data_base[$i++] = array('実績' , '売上' ,   '本年'     ); // 
		$data_base[$i++] = array('実績' , '売上' ,   '前年'     ); // 
		$data_base[$i++] = array('実績' , '売上' ,   '前年比'   ); // 
		$data_base[$i++] = array('実績' , '売上' ,   '前年差異' ); // 
		
		$data_base[$i++] = array('実績' , '客数' ,  '本年'     ); // 
		$data_base[$i++] = array('実績' , '客数' ,  '前年'     ); // 
		$data_base[$i++] = array('実績' , '客数' ,  '前年比'   ); // 
		$data_base[$i++] = array('実績' , '客数' ,  '前年差異' ); // 
		
		$data_base[$i++] = array('実績' , '客単価' , '本年'     ); // 
		$data_base[$i++] = array('実績' , '客単価' , '前年'     ); // 
		$data_base[$i++] = array('実績' , '客単価' , '前年比'   ); // 
		$data_base[$i++] = array('実績' , '客単価' , '前年差異' ); // 
		
		$index_cost = $i;
		$data_base[$i++] = array('実績' , '経費' , '本年'     ); // 
		$data_base[$i++] = array('実績' , '経費' , '前年'     ); // 
		$data_base[$i++] = array('実績' , '経費' , '前年比'   ); // 
		$data_base[$i++] = array('実績' , '経費' , '前年差異' ); // 
		
		$indexB["qi"]["1"] = $i ;
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'タイトル' ); // 
		$data_base[$i++] = array('チラシ' , 'チラシ' , '折込日' ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , '管理番号表' ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , '管理番号裏' ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'B2見本部数' ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'B2部数'   ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'B3見本部数' ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'B3部数'   ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'B4見本部数' ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , 'B4部数'   ); //
		$data_base[$i++] = array('チラシ' , 'チラシ' , '折込パターン' ); //  
		
		$indexB["lo"]["1"] = $i ;
		$data_base[$i++] = array('地域広告' ,'地域広告1', 'タイトル' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告1', '折込日' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告1', '管理番号表' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告1', '管理番号裏' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告1', 'B2部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告1', 'B3部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告1', 'B4部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告1', '地域新聞名'  ); //
		
		$indexB["lo"]["2"] = $i ;
		$data_base[$i++] = array('地域広告' ,'地域広告2' , 'タイトル' ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告2', '折込日' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告2', '管理番号表' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告2', '管理番号裏' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告2', 'B2部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告2', 'B3部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告2', 'B4部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告2', '地域新聞名'  ); //
		
		$indexB["lo"]["3"] = $i ;
		$data_base[$i++] = array('地域広告' ,'地域広告3', 'タイトル' ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告3', '折込日' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告3', '管理番号表' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告3', '管理番号裏' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告3', 'B2部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告3', 'B3部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告3', 'B4部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告3', '地域新聞名'  ); //
		
		$indexB["lo"]["4"] = $i ;
		$data_base[$i++] = array('地域広告' ,'地域広告4', 'タイトル' ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告4', '折込日' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告4', '管理番号表' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告4', '管理番号裏' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告4', 'B2部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告4', 'B3部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告4', 'B4部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告4', '地域新聞名'  ); //
		
		$indexB["lo"]["5"] = $i ;
		$data_base[$i++] = array('地域広告' ,'地域広告5', 'タイトル' ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告5', '折込日' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告5', '管理番号表' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告5', '管理番号裏' ); //
		$data_base[$i++] = array('地域広告' ,'地域広告5', 'B2部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告5', 'B3部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告5', 'B4部数'   ); // 
		$data_base[$i++] = array('地域広告' ,'地域広告5', '地域新聞名'  ); //
		
		$indexB["ji"]["sale"] = $i;
		$data_base[$i++] = array('DM' , 'V圧着'      ,   'タイトル' ); // 
		$data_base[$i++] = array('DM' , 'V圧着'      ,   '投関数'   ); //
		 
		$data_base[$i++] = array('DM' , 'Z圧着'      ,   'タイトル'   ); // 
		$data_base[$i++] = array('DM' , 'Z圧着'      ,   '投関数'   ); //
		
		$data_base[$i++] = array('DM' , '定型ハガキ' ,   'タイトル'   ); // 
		$data_base[$i++] = array('DM' , '定型ハガキ' ,   '投関数'   ); // 
		
		$data_base[$i++] = array('DM' , '封書'       ,   'タイトル'   ); // 
		$data_base[$i++] = array('DM' , '封書'       ,   '投関数'   ); // 
		
		$data_base[$i++] = array('DM' , '封書(タウンプラス)'  , 'タイトル'  ); // 
		$data_base[$i++] = array('DM' , '封書(タウンプラス)'  , '投関数'   ); // 
		
		//列名
		$colums = array();
		//1 string code 2 integer 3 date 4 string ""必要 
		
		$col_heads = array(
			'sum'   => array( '年度合計',  2),
			'sum1'  => array( '上期合計',  2),
			'sum2'  => array( '下期合計',  2)
		);
		
		$datasum = initsum($indexB, $col_heads);
		$col_sum_pre_half = array();
		foreach ($week as $k => $v ){
			$col_heads[$k]  = array($v["disp_week"], 1) ;
			if ($v["week_date_begin"] > $year4."1000") {
				$col_sum_pre_half[$k] = FALSE;
			} else {
				$col_sum_pre_half[$k] = TRUE; ;
			}
		}
		
		//列名
		foreach ($col_heads as $k=>$v) {
			$colums["name"][]   = $k;
			$colums["head"][]   = $v[0];
			$colums["type"][$k] = $v[1];
		}
		$is_final = TRUE;
		
		$details = $dao->getCellDetailsAll( $matrix_cd_f, $matrix_cd_t,$is_final, $aja_cd, $formshops );
		$shops   = $dao->getShops($aja_cd, $formshops);
		$data_rez  = $dao->getshopsRez($matrix_cd_f, $matrix_cd_t,$aja_cd,  $formshops);
		$data_dm  = $dao->getshopsDm($year4, $matrix_cd_f, $matrix_cd_t,$aja_cd,  $formshops);
		$data_last  = $dao->getshopsCostLast($year4, $matrix_cd_f, $matrix_cd_t,$aja_cd,  $formshops);
		//価格
		$size_prices = $dao->getQrsPriceShopSize($year4, $aja_cd);
		$patt_prices =  $dao->getAreaPatternPrice($year4);
		
		$data    = array();
		$datashops = array();
		foreach ($details as $kshop => $vshop) {
			//
			$data_detail = array();
			$data_detail = $datasum;
			foreach ($vshop as $k => $v) {
				$matrix_cd = $v["matrix_cd"]; 
				$ddIndex = $indexB[$v["type"]][$v["type_index"]];
				if ($ddIndex<10) {
					continue;
				}
				$data_detail[$ddIndex][$matrix_cd]        = $v["title"] ; //タイトル
				$data_detail[$ddIndex + 1][ $matrix_cd ]  = $v["exp_ins_week"] ;//折込日
				$data_detail[$ddIndex + 2][ $matrix_cd ]  = $v["mano_out"] ;//管理番号表
				$data_detail[$ddIndex + 3][ $matrix_cd ]  = $v["mano_in"] ;//管理番号裏
				$patter_index = $ddIndex + 7;
				$ddSize  = $ddIndex + 3 + $v["size_index"];
				
				if ($ddIndex < 20) {
					//通常チラシ
					$patter_index = $ddIndex + 10;
					$ddSize  = $ddIndex + 4 + ($v["size_index"] -1) * 2 ;
					$data_detail[$ddSize][ $matrix_cd ]     = $v["sample"] ;
					$data_detail[$ddSize + 1][ $matrix_cd ] = $v["pnums"] ;
					
					$pnums   = (empty($v["pnums"]) ? 0 : $v["pnums"]);
					$psample = (empty($v["sample"]) ? 0 : $v["sample"]);
					$cost = ($psample +$pnums) * $size_prices[$kshop][$v["size"]] ;
					$data_detail[$index_cost] [$matrix_cd] = $data_detail[$index_cost] [$matrix_cd] + $cost;
					
				}else {
					//地域チラシ
					$data_detail[$ddSize] [ $matrix_cd ] = $v["pnums"] ;
					$pnums   = (empty($v["pnums"]) ? 0 : $v["pnums"]);
					$cost = $pnums * $patt_prices[$v["pattern_cd"]][$v["size"]];
					$data_detail[$index_cost] [$matrix_cd] = $data_detail[$index_cost] [$matrix_cd] + $cost;
				}
				$data_detail[$patter_index][$matrix_cd]  = $v["pattern_name"] ;
			}
			
			foreach ($week as $k => $v ){
				$matrix_cd = $k;
				$oReDm = $data_rez[$kshop][$matrix_cd];
				$i = 0;
				$data_detail[$i++] [$matrix_cd]  = $oReDm["now_quantity"];//'実績' , '売上' ,   '本年'     ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["last_quantity"];//'実績' , '売上' ,   '前年'     ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["rate_quantity"];//'実績' , '売上' ,   '前年比'   ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["diff_quantity"];//'実績' , '売上' ,   '前年差異' ); // 
				
				$data_detail[$i++] [$matrix_cd]  = $oReDm["now_sales"];//'実績' , '客数' ,  '本年'     ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["last_sales"];//'実績' , '客数' ,  '前年'     ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["rate_sales"];//'実績' , '客数' ,  '前年比'   ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["diff_sales"];//'実績' , '客数' ,  '前年差異' ); // 
				
				$data_detail[$i++] [$matrix_cd]  = $oReDm["now_price"]; //'実績' , '客単価' , '本年'     ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["last_price"]; //'実績' , '客単価' , '前年'     ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["rate_price"];//'実績' , '客単価' , '前年比'   ); // 
				$data_detail[$i++] [$matrix_cd]  = $oReDm["diff_price"];//'実績' , '客単価' , '前年差異' ); //
				
				
				$oDm = $data_dm[$kshop][$matrix_cd];
				$i = $indexB["ji"]["sale"] ;
				
				$data_detail[$i++] [$matrix_cd]  = $oDm["t1"];  // DM' , 'V圧着'      ,   'タイトル' ); // 
				$data_detail[$i++] [$matrix_cd]  = $oDm["c1"];  // DM' , 'V圧着'      ,   '投関数'   ); //
				 
				$data_detail[$i++] [$matrix_cd]  = $oDm["t2"];  // DM' , 'Z圧着'      ,   'タイトル'   ); // 
				$data_detail[$i++] [$matrix_cd]  = $oDm["c2"];  // DM' , 'Z圧着'      ,   '投関数'   ); //
				
				$data_detail[$i++] [$matrix_cd]  = $oDm["t3"];  // DM' , '定型ハガキ' ,   'タイトル'   ); // 
				$data_detail[$i++] [$matrix_cd]  = $oDm["c3"];  // DM' , '定型ハガキ' ,   '投関数'   ); // 
				
				$data_detail[$i++] [$matrix_cd]  = $oDm["t4"];  // DM' , '封書'       ,   'タイトル'   ); // 
				$data_detail[$i++] [$matrix_cd]  = $oDm["c4"];  // DM' , '封書'       ,   '投関数'   ); // 
				
				$data_detail[$i++] [$matrix_cd]  = $oDm["t5"];  // DM' , '封書(タウンプラス)'  , 'タイトル'  ); // 
				$data_detail[$i++] [$matrix_cd]  = $oDm["c5"];  // DM' , '封書(タウンプラス)'  , '投関数'   ); //
				
				$data_detail[$index_cost] [$matrix_cd] = Class_PWD::cost_round ($data_detail[$index_cost] [$matrix_cd]);
				$data_detail[$index_cost + 1] [$matrix_cd]  = Class_PWD::cost_round ($data_last[$kshop][$matrix_cd]);//'実績' , '経費' , '前年'     ); //
				
				if (!empty( $data_detail[$index_cost + 1] [$matrix_cd] )){
					$data_detail[$index_cost + 2] [$matrix_cd]  =  Class_PWD::cost_round ( $data_detail[$index_cost] [$matrix_cd]/ $data_detail[$index_cost + 1] [$matrix_cd]); //;
				}else {
					$data_detail[$index_cost + 2] [$matrix_cd]  = "0.00";
				}
				
				$data_detail[$index_cost + 3] [$matrix_cd] = Class_PWD::cost_round ($data_detail[$index_cost] [$matrix_cd] - $data_detail[$index_cost + 1] [$matrix_cd] );
				
				
				if ($col_sum_pre_half[ $matrix_cd] ) {
					foreach ($datasum as $ii =>$iv){
						$data_detail[$ii]["sum1"] = $data_detail[$ii]["sum1"] + $data_detail[$ii][$matrix_cd];
					}
					
				}else {
					foreach ($datasum as $ii =>$iv){
						$data_detail[$ii]["sum2"] = $data_detail[$ii]["sum2"] +  $data_detail[$ii][$matrix_cd];
					}
				} 
				
				/*
				$j=0;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '売上' ,   '本年'     ); 
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '売上' ,   '前年'     );
				$j++ ;
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '売上' ,   '前年差異' );
				
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '客数' ,   '本年'     ); 
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '客数' ,   '前年'     );
				$j++ ;
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '客数' ,   '前年差異' );
				
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '客単価' ,   '本年'     ); 
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '客単価' ,   '前年'     );
				$j++ ;
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '客単価' ,   '前年差異' );
				
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '経費' ,   '本年'     ); 
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '経費' ,   '前年'     );
				$j++ ;
				$j++ ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd]; //実績' , '経費' ,   '前年差異' );
				
				
				$j = $indexB["ji"]["sale"] + 1 ;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd];
				$j = $j + 2;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd];
				$j = $j + 2;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd];
				$j = $j + 2;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum"] +  $data_detail[$j][$matrix_cd];
				
				$j = $index_cost;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum1"] +  $data_detail[$j][$matrix_cd];
				$j ++;
				$data_detail[$j]["sum"] = $data_detail[$j]["sum1"] +  $data_detail[$j][$matrix_cd];
			
				//
				*/
			}
			
			foreach ($datasum as $ii =>$iv){
				$data_detail[$ii]["sum"] = $data_detail[$ii]["sum1"] +  $data_detail[$ii]["sum2"];
				
			}
			
			$data[$kshop]      = $data_detail;
			$nowshops          = $shops[$kshop];
			$datashops[$kshop] = array($nowshops["area_name"], $nowshops["zone_name"], $nowshops["shop_cd"], $nowshops["shop_name"] );
		}
		
		//再整形
		foreach ($data as $k=>$v) {
			
		}
	//データ整形完了
		output_csv($data_base, $datashops,$data, $colums);
		exit;
	}
}
//---CSVダウンロード　処理完了
$year4arr = Class_M_OTHER::get_years_array();
if (empty($forms["year4"])) {
	$forms["year4"] =    current($year4arr);
}
if( isset($forms) )       $smarty->assign('forms', $forms);
if( isset($zoneuser) )    $smarty->assign('zone', $zoneuser);

$smarty->assign('cellsort', $cellsort );

if( isset($areauser) )    $smarty->assign('areauser', $areauser);
if( isset( $err_mes ) )   $smarty->assign('err', $err_mes);

$smarty->assign('year4',  $year4arr);

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR ,FALSE, TRUE);
$smarty->assign('week', $week );

$smarty->assign('authtg2', $authtg2 );
$smarty->assign('authtg3', $authtg3 );
$smarty->assign('authtg4', $authtg4 );


$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('tmoncellshop_index.html');

function  output_csv($database, $datashops, $data, $colums )
{
	echo   mb_convert_encoding("エリア名,ゾーン名,店舗コード,店舗名,大分類,サブ分類,項目,", "SJIS-win", "UTF-8");
	echo   mb_convert_encoding(implode(',',  $colums["head"]), "SJIS-win", "UTF-8")."\n";
	
	foreach ($data as $kshop => $vshop) {
		foreach ($database as $bk => $bv) {
			echo  mb_convert_encoding(implode(',', $datashops[$kshop]), "SJIS-win", "UTF-8").",";
			echo  mb_convert_encoding(implode(',', $bv).',', "SJIS-win", "UTF-8");
			$v = isset($vshop[$bk]) ? $vshop[$bk] : NULL ;
			$linearr  = array();
			foreach ($colums["name"]  as $ck => $cv) {
				if ($colums["type"][$cv] == 4) {
					$linearr[]= '"'.$v[$cv].'"';
				} else {
					$linearr[]=isset($v[$cv]) ? $v[$cv] : '';
				}
			}
			echo  mb_convert_encoding(implode(',', $linearr), "SJIS-win", "UTF-8")."\n";
		}
	}
}

function  initsum($indexB, $col_heads) {
	$datasum = array();
	foreach ($col_heads as $v =>  $value) {
		$j=0;
		$datasum[$j++][$v] = 0; //実績' , '売上' ,   '本年'     ); // 
		$datasum[$j++][$v] = 0; //実績' , '売上' ,   '前年'     ); // 
		$j++; 					//実績' , '売上' ,   '前年比'   ); // 
		$datasum[$j++][$v] = 0; //実績' , '売上' ,   '前年差異' ); // 
		
		$datasum[$j++][$v] = 0; //実績' , '客数' ,  '本年'     ); // 
		$datasum[$j++][$v] = 0; //実績' , '客数' ,  '前年'     ); // 
		$j++; 					//実績' , '客数' ,  '前年比'   ); // 
		$datasum[$j++][$v] = 0; //実績' , '客数' ,  '前年差異' ); // 
		
		$datasum[$j++][$v] = 0; //実績' , '客単価' , '本年'     ); // 
		$datasum[$j++][$v] = 0; //実績' , '客単価' , '前年'     ); // 
		$j++; 					//実績' , '客単価' , '前年比'   ); // 
		$datasum[$j++][$v] = 0; //実績' , '客単価' , '前年差異' ); // 
		
		$datasum[$j++][$v] = 0; //実績' , '経費' , '本年'     ); // 
		$datasum[$j++][$v] = 0; //実績' , '経費' , '前年'     ); // 
		$j++; 					//実績' , '経費' , '前年比'   ); // 
		$datasum[$j++][$v] = 0; //実績' , '経費' , '前年差異' ); //
		
		$j = $indexB["qi"]["1"] + 4;
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0;
		$datasum[$j++][$v] = 0;
		$datasum[$j++][$v] = 0;
		$datasum[$j++][$v] = 0;

		$j = $indexB["lo"]["1"] + 4;
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0;

		$j = $indexB["lo"]["2"] + 4;
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0;

		$j = $indexB["lo"]["3"] + 4;
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0;

		$j = $indexB["lo"]["4"] + 4;
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0;

		$j = $indexB["lo"]["5"] + 4;
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0; 
		$datasum[$j++][$v] = 0;
		
		//DM
		$j = $indexB["ji"]["sale"] + 1 ;
		$datasum[$j][$v] = 0;
		$j = $j + 2;
		$datasum[$j][$v] = 0;
		$j = $j + 2;
		$datasum[$j][$v] = 0;
		$j = $j + 2;
		$datasum[$j][$v] = 0;
	}
	return $datasum;
}
?>